// 判断：当前用户使用页面的时候，看你有没有 token;直接转跳index;
if (localStorage.getItem("token") == null) {
    location.href = "/login.html";
}



function getInfo() {
    $.ajax({
        url: '/my/userinfo',
        // 设置请求头 需要添加的数据
        headers: {
            Authorization: localStorage.getItem('token')
        },
        success: function(res) {
            if (res.status == 0) {
                let obj = res.data

                //头像
                if (obj.user_pic != null) {
                    $('userinfo img').show().prop('src', obj.user_pic)
                }

                //昵称
                else if (obj.nickname != '') {
                    let t = obj.nickname.substr(0, 1).toUpperCase()
                    $('.userinfo .avatar').css('display', 'inline-block').text(t)
                }
                //账户
                else if (obj.username != '') {
                    let t = obj.username.substr(0, 1).toUpperCase()
                    $('.userinfo .avatar').css('display', 'inline-block').text(t)
                }
                $('.username').text(obj.nickname || obj.username)
            }
        },
    })
}
getInfo()



//退出登录
let layer = layui.layer

//注册事件
$('#logout').on('click', function() {

    //弹窗提示
    layer.confirm('确认退出', function(index) {
        layer.close(index)

        //返回初始界面
        location.href = '/login.html'

        //清除token
        localStorage.removeItem('token')
    })
})